// You need to add 1 line of code at each of the 5 locations
// identified by embedded comments
package infotool;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class InfoView{
    private InfoModel model;
    private static int viewNo=0;
    private int viewId;
    // GUI Components
    private JFrame frame;
    private Container contentPane;
    private JPanel msgPan = new JPanel();
    private JPanel wthPan = new JPanel();
    private JTextField msgText = new JTextField(30);
    private JTextField wthText = new JTextField(30);
    private JLabel msgLabel = new JLabel("Message Display Page");
    private JLabel wthLabel = new JLabel("Weather Display Page");
    private JButton msgUpdate = new JButton("Update Message");
    private JButton wthGet = new JButton("Get Weather");
    private JButton wthUpdate = new JButton("Update Weather");
    private JButton msgGet = new JButton("Get Message");
    private CardLayout card = new CardLayout();
    
    // user gesture listener management data structure
    private ArrayList<InfoController> listeners = new ArrayList<InfoController>(10);
    
    public InfoView(InfoModel model) {
        this.model = model;
        viewId = ++viewNo;
        System.out.println("View " + viewId +
                ": InfoDisplay(model) ");
        buildDisplay();
        //** 1 Students action required: There is a missing line of code
        //**   Hint - The view should register itself as a listener
        //**   of changes to information stored in the model.
        //**   Another hint - Call the addModelChangeListener method
        //**   of the model object and pass it this view object.
        
    }
    
    public void processModelChange(Object event){
        System.out.println("View " + viewId +
                ": processModelChange() ");
        refresh();
    }
    
    public void addUserGestureListener(InfoController con){
        System.out.println("View " + viewId +
                ": addUserGestureListener(controller) ");
        listeners.add(con);
    }
    
    
    public void showDisplay(String display){
        System.out.println("View " + viewId +
                ": showDisplay() ");
        card.show(contentPane, display);
    }
    
    //private methods
    
    private void refresh() {
        System.out.println("View " + viewId +
                ": refresh() ");
        msgText.setText(model.getMessage());
        wthText.setText(model.getWeather());
    }
    private void buildDisplay(){
        frame = new JFrame("Info Client " + viewId);
        contentPane = frame.getContentPane();
        // build message panel msgPan
        msgPan.setLayout(new BorderLayout());
        msgPan.add(msgLabel, BorderLayout.NORTH);
        msgPan.add(msgText, BorderLayout.CENTER);
        msgPan.add(msgUpdate, BorderLayout.EAST);
        msgPan.add(wthGet, BorderLayout.WEST);
        // build weather panel wthPan
        wthPan.setLayout(new BorderLayout());
        wthPan.add(wthLabel, BorderLayout.NORTH);
        wthPan.add(wthText, BorderLayout.CENTER);
        wthPan.add(wthUpdate, BorderLayout.EAST);
        wthPan.add(msgGet, BorderLayout.WEST);
        // add panels to frame and show frame
        contentPane.setLayout(card);
        contentPane.add(msgPan, "message");
        contentPane.add(wthPan, "weather");
        frame.pack();
        frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
        frame.setVisible( true );
        frame.setLocation(0, (viewId-1)*100);
        refresh();
        // register event listeners with the GUI controls
        msgUpdate.addActionListener(msgUpdateHandler);
        wthGet.addActionListener(wthGetHandler);
        wthUpdate.addActionListener(wthUpdateHandler);
        msgGet.addActionListener(msgGetHandler);
    }
    
    //event handler methods
    ActionListener msgUpdateHandler =
            new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            // fire event -- inform registered controllers
            for (InfoController con: listeners) {
                System.out.println("View " + viewId +
                        ": fire msgUpdate event");
                //** students action required: Fill missing line
                //** 1 Inform the controller object con that a user has
                //**   pressed the "Update Message" button
                //**   Hint - Call the processMsgUpdate method of the
                //**   controller object con, and pass it the input text
                //**   on the textfield msgText.(ie msgText.getText())
                
            }
        }
    };
    
    ActionListener msgGetHandler =
            new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            // code to process add new customer request
            // fire event -- inform registered controllers
            for (InfoController con: listeners) {
                System.out.println("View " + viewId +
                        ": fire getMessage event");
                // student action required -- what else should this method do
                //** students action required: Fill missing line
                //** 1 Inform the controller object con, that a user has
                //**   pressed the "Get Message" button
                //**   Hint - Call the processGetMessage method of the
                //**   controller object con.
                
            }
        }
    };
    
    ActionListener wthUpdateHandler =
            new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            // code to process add new customer request
            // fire event -- inform registered controllers
            for (InfoController con: listeners) {
                System.out.println("View " + viewId +
                        ": fire wthUpdate event");
                //** students action required: Fill missing line
                //** 1 Inform the controller object con, that a user has
                //**   pressed the "Update Weather" button
                //**   Hint - Call the processWthUpdate method of the
                //**   controller object con and pass it the input text
                //**   on the textfield wthText.(ie wthText.getText())
                
            }
        }
    };
    
    ActionListener wthGetHandler =
            new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            // code to process add new customer request
            // fire event -- inform registered controllers
            for (InfoController con: listeners) {
                System.out.println("View " + viewId +
                        ": fire getWeather event");
                //** students action required: Fill missing line
                //** 1 Inform the controller object con, that a user has
                //**   pressed the "Get Weather" button
                //**   Hint - Call the processGetWeather method of the
                //**   controller object con.
                
            }
        }
    };
}

